using System;
using System.Collections.Generic;
using System.Text;

namespace MCromwell.StaffIntranet.Core
{
    /// <summary>
    /// Staff object represents staff in domain
    /// Compulsory fields are:
    /// 
    /// - lastName
    /// - firstName
    /// - emailAddress
    /// - telephoneNumber
    /// - Extension
    /// </summary>
    public class Staff : IHasValidation
    {
        private int id = 0;
        private string lastName = string.Empty;
        private string middleName = string.Empty;
        private string firstName = string.Empty;
        private Email emailAddress = null;
        private string telephoneNumber = string.Empty;
        private string extensionNumber = string.Empty;
        private Location location = new Location();
        private byte[] photo;
                        
        public Staff()
        {
            
        }
                                     
        public virtual string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public virtual string MiddleName
        {
            get { return middleName; }
            set { middleName = value; }
        }

        public virtual string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public virtual string EmailAddress
        {
            get { return emailAddress.Value; }
            set {
                if (emailAddress == null)
                    emailAddress = new Email(value);
                else
                    emailAddress.Set(value); 
            }
        }

        public virtual string TelephoneNumber
        {
            get { return telephoneNumber; }
            set { telephoneNumber = value; }
        }

        public virtual string Extension
        {
            get { return extensionNumber; }
            set { extensionNumber = value; }
        }

        public virtual int Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual Location Location
        {
            get { return location; }
            set { location = value; }
        }

        public virtual byte[] Photo
        {
            get { return photo; }
            set { photo = value; }
        }
                                
        public virtual DomainObjectValidationResult Validate()
        {
            DomainObjectValidationResult result = new DomainObjectValidationResult();
                       
            ValidateAgainstNullOrEmpty(lastName, "lastname", result);
            ValidateAgainstNullOrEmpty(firstName,"firstname", result);
            ValidateAgainstNullOrEmpty(extensionNumber, "extension", result);
            ValidateAgainstNullOrEmpty(telephoneNumber, "telephonenumber", result);
            ValidateAgainstNullOrEmpty(location, "location", result);

            DomainObjectValidationResult emailResult = emailAddress.Validate();
            if (!emailResult.IsValid)
                result.Merge(emailResult.Results);
            
            return result;
                        
        }

        private void ValidateAgainstNullOrEmpty(string stringToCheck, string valdationId, DomainObjectValidationResult result)
        {
            if (string.IsNullOrEmpty(stringToCheck))
                result.Register(new ValidationResultItem(valdationId, string.Format("{0} cannot be null or left empty", valdationId)));
        }

        private void ValidateAgainstNullOrEmpty<T>(T objectToCheck , string validationId, DomainObjectValidationResult result) where T : class
        {
            if (objectToCheck == null)
                result.Register(new ValidationResultItem(validationId, string.Format("{0} cannot be null", validationId)));

        }

    }
}
